<template>
  <div class="hm-calendar">
    <el-card>
      <template #header>
        <div>
          工作日历
        </div>
      </template>
      <div style="text-align:right">
        <el-select v-model="year" @change="changeEvent">
          <el-option
            v-for="item in yearArr"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
        <el-select v-model="month" @change="changeEvent">
          <el-option
            v-for="item in monthArr"
            :key="item"
            :label="item"
            :value="item"
          ></el-option>
        </el-select>
      </div>
      <el-calendar v-model="value">
        <template #dateCell="{data,date}">
          <div @click="cellClick(date)" style="width:100%;height:100%;">
            <span>{{ data.day.split('-')[2] }}</span>
            <!-- <span v-if="data.type === 'current-month'">{{
            date | formatDate
          }}</span> -->
            <span v-if="formatDate(date) && data.type === 'current-month'"
              >休</span
            >
          </div>
        </template>
      </el-calendar>
    </el-card>
  </div>
</template>
<script>
export default {
  props: {
    valueTime: {
      type: Date,
      default: () => {
        return new Date()
      }
    }
  },
  model: {
    prop: 'valueTime'
  },
  watch: {
    valueTime: {
      deep: true, // 监听堆与栈的变化
      immediate: true, // 第一次绑定就立马执行回调函数
      handler (newVal) {
        this.value = newVal
        this.year = newVal.getFullYear()
        this.month = newVal.getMonth() + 1
        // 年的时间范围，给一个当前传入的年的前五年与后五年内
        const min = this.year - 5
        const max = this.year + 5
        let arr = []
        for (let i = min; i <= max; i++) {
          arr.push(i)
        }
        this.yearArr = arr
      }
    }
  },
  data () {
    return {
      value: new Date(),
      year: '',
      yearArr: [],
      month: '',
      monthArr: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
    }
  },
  filters: {
    // formatDate (time) {
    //   if (time.getDay() === 0 || time.getDay() === 6) {
    //     return '休'
    //   } else {
    //     return ''
    //   }
    // }
  },
  methods: {
    formatDate (time) {
      if (time.getDay() === 0 || time.getDay() === 6) {
        return true
      } else {
        return false
      }
    },
    changeEvent () {
      this.$emit('input', new Date(this.year + '-' + this.month + '-01'))
    },
    cellClick (date) {
      this.$emit('input', date)
    }
  }
}
</script>
<style lang="scss" scoped>
.hm-calendar {
  ::v-deep .el-calendar__header {
    display: none;
  }
}
</style>
